package com.lyh.system.service.impl;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lyh.common.core.domain.AjaxResult;
import com.lyh.common.enums.RelationTypeEnum;
import com.lyh.common.exception.ServiceException;
import com.lyh.common.utils.StringUtils;
import com.lyh.system.domain.AssetEvidence;
import com.lyh.system.domain.SysAreaOrder;
import com.lyh.system.domain.req.*;
import com.lyh.system.domain.vo.AppUpdateVo;
import com.lyh.system.mapper.AssetEvidenceMapper;
import com.lyh.system.mapper.SysAreaOrderMapper;
import com.lyh.system.service.ISysAreaOrderService;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import com.lyh.system.mapper.SysRelationValueMapper;
import com.lyh.system.domain.SysRelationValue;
import com.lyh.system.service.ISysRelationValueService;
import org.springframework.transaction.annotation.Transactional;

import static com.lyh.common.utils.SecurityUtils.getLoginUser;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author lyh
 * @date 2023-04-26
 */
@Service
public class SysRelationValueServiceImpl implements ISysRelationValueService
{
    @Autowired
    private SysRelationValueMapper sysRelationValueMapper;

    @Autowired
    private SysAreaOrderMapper sysAreaOrderMapper;

    @Autowired
    private AssetEvidenceMapper assetEvidenceMapper;


    @Autowired
    @Lazy
    private  ISysAreaOrderService sysAreaOrderService;

    /**
     * 查询【请填写功能名称】
     *
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public SysRelationValue selectSysRelationValueById(Long id)
    {
        return sysRelationValueMapper.selectSysRelationValueById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param sysRelationValue 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<SysRelationValue> selectSysRelationValueList(SysRelationValue sysRelationValue)
    {
        return sysRelationValueMapper.selectSysRelationValueList(sysRelationValue);
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param sysRelationValue 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertSysRelationValue(SysRelationValue sysRelationValue)
    {
        return sysRelationValueMapper.insertSysRelationValue(sysRelationValue);
    }

    @Override
    @Transactional
    public int imSaveOrUpdateBdfy(SysImmediatelyReq req) {

        if(StringUtils.isNotEmpty(req.getImmediately())){
            sysRelationValueMapper.delete(new LambdaQueryWrapper<SysRelationValue>()
                    .eq(SysRelationValue::getCode,RelationTypeEnum.Immediately.getCode() )
                    .eq(SysRelationValue::getBusinessId,RelationTypeEnum.BDFYSZ.getCode()));
            SysRelationValue sysRelationValue = new SysRelationValue();
            sysRelationValue.setBusinessId(RelationTypeEnum.BDFYSZ.getCode());
            sysRelationValue.setCode(RelationTypeEnum.Immediately.getCode());
            sysRelationValue.setValue(req.getImmediately());
            sysRelationValueMapper.insert(sysRelationValue);

        }

        if(StringUtils.isNotEmpty(req.getEveryV0())){
            sysRelationValueMapper.delete(new LambdaQueryWrapper<SysRelationValue>()
                    .eq(SysRelationValue::getCode,RelationTypeEnum.EveryV0.getCode() )
                    .eq(SysRelationValue::getBusinessId,RelationTypeEnum.BDFYSZ.getCode()));
            SysRelationValue sysRelationValue = new SysRelationValue();
            sysRelationValue.setBusinessId(RelationTypeEnum.BDFYSZ.getCode());
            sysRelationValue.setCode(RelationTypeEnum.EveryV0.getCode());
            sysRelationValue.setValue(req.getEveryV1());
            sysRelationValueMapper.insert(sysRelationValue);
        }

        if(StringUtils.isNotEmpty(req.getEveryV1())){
            sysRelationValueMapper.delete(new LambdaQueryWrapper<SysRelationValue>()
                    .eq(SysRelationValue::getCode,RelationTypeEnum.EveryV1.getCode() )
                    .eq(SysRelationValue::getBusinessId,RelationTypeEnum.BDFYSZ.getCode()));
            SysRelationValue sysRelationValue = new SysRelationValue();
            sysRelationValue.setBusinessId(RelationTypeEnum.BDFYSZ.getCode());
            sysRelationValue.setCode(RelationTypeEnum.EveryV1.getCode());
            sysRelationValue.setValue(req.getEveryV1());
            sysRelationValueMapper.insert(sysRelationValue);
        }
        if(StringUtils.isNotEmpty(req.getEveryV2())){
            sysRelationValueMapper.delete(new LambdaQueryWrapper<SysRelationValue>()
                    .eq(SysRelationValue::getCode,RelationTypeEnum.EveryV2.getCode() )
                    .eq(SysRelationValue::getBusinessId,RelationTypeEnum.BDFYSZ.getCode()));
            SysRelationValue sysRelationValue = new SysRelationValue();
            sysRelationValue.setBusinessId(RelationTypeEnum.BDFYSZ.getCode());
            sysRelationValue.setCode(RelationTypeEnum.EveryV2.getCode());
            sysRelationValue.setValue(req.getEveryV2());
            sysRelationValueMapper.insert(sysRelationValue);
        }
        if(StringUtils.isNotEmpty(req.getEveryV3())){
            sysRelationValueMapper.delete(new LambdaQueryWrapper<SysRelationValue>()
                    .eq(SysRelationValue::getCode,RelationTypeEnum.EveryV3.getCode() )
                    .eq(SysRelationValue::getBusinessId,RelationTypeEnum.BDFYSZ.getCode()));
            SysRelationValue sysRelationValue = new SysRelationValue();
            sysRelationValue.setBusinessId(RelationTypeEnum.BDFYSZ.getCode());
            sysRelationValue.setCode(RelationTypeEnum.EveryV3.getCode());
            sysRelationValue.setValue(req.getEveryV3());
            sysRelationValueMapper.insert(sysRelationValue);
        }
        if(StringUtils.isNotEmpty(req.getEveryV4())){
            sysRelationValueMapper.delete(new LambdaQueryWrapper<SysRelationValue>()
                    .eq(SysRelationValue::getCode,RelationTypeEnum.EveryV4.getCode() )
                    .eq(SysRelationValue::getBusinessId,RelationTypeEnum.BDFYSZ.getCode()));
            SysRelationValue sysRelationValue = new SysRelationValue();
            sysRelationValue.setBusinessId(RelationTypeEnum.BDFYSZ.getCode());
            sysRelationValue.setCode(RelationTypeEnum.EveryV4.getCode());
            sysRelationValue.setValue(req.getEveryV4());
            sysRelationValueMapper.insert(sysRelationValue);
        }
        if(StringUtils.isNotEmpty(req.getEveryV5())){
            sysRelationValueMapper.delete(new LambdaQueryWrapper<SysRelationValue>()
                    .eq(SysRelationValue::getCode,RelationTypeEnum.EveryV5.getCode() )
                    .eq(SysRelationValue::getBusinessId,RelationTypeEnum.BDFYSZ.getCode()));
            SysRelationValue sysRelationValue = new SysRelationValue();
            sysRelationValue.setBusinessId(RelationTypeEnum.BDFYSZ.getCode());
            sysRelationValue.setCode(RelationTypeEnum.EveryV5.getCode());
            sysRelationValue.setValue(req.getEveryV5());
            sysRelationValueMapper.insert(sysRelationValue);
        }
        return 1;

    }

    /*

        收益比例
    */
    @Override
    public int imSaveOrUpdateBdfyMember(SysImmediatelyReq req) {
        if(StringUtils.isNotEmpty(req.getImmediately())){
            sysRelationValueMapper.delete(new LambdaQueryWrapper<SysRelationValue>()
                    .eq(SysRelationValue::getCode,RelationTypeEnum.ImmediatelyM.getCode() )
                    .eq(SysRelationValue::getBusinessId,RelationTypeEnum.BDFYSZ_MEMBER.getCode()));
            SysRelationValue sysRelationValue = new SysRelationValue();
            sysRelationValue.setBusinessId(RelationTypeEnum.BDFYSZ_MEMBER.getCode());
            sysRelationValue.setCode(RelationTypeEnum.ImmediatelyM.getCode());
            sysRelationValue.setValue(req.getImmediately());
            sysRelationValueMapper.insert(sysRelationValue);

        }

        if(StringUtils.isNotEmpty(req.getEveryV0())){
            sysRelationValueMapper.delete(new LambdaQueryWrapper<SysRelationValue>()
                    .eq(SysRelationValue::getCode,RelationTypeEnum.EveryV0M.getCode() )
                    .eq(SysRelationValue::getBusinessId,RelationTypeEnum.BDFYSZ_MEMBER.getCode()));
            SysRelationValue sysRelationValue = new SysRelationValue();
            sysRelationValue.setBusinessId(RelationTypeEnum.BDFYSZ_MEMBER.getCode());
            sysRelationValue.setCode(RelationTypeEnum.EveryV0M.getCode());
            sysRelationValue.setValue(req.getEveryV1());
            sysRelationValueMapper.insert(sysRelationValue);
        }

        if(StringUtils.isNotEmpty(req.getEveryV1())){
            sysRelationValueMapper.delete(new LambdaQueryWrapper<SysRelationValue>()
                    .eq(SysRelationValue::getCode,RelationTypeEnum.EveryV1M.getCode() )
                    .eq(SysRelationValue::getBusinessId,RelationTypeEnum.BDFYSZ_MEMBER.getCode()));
            SysRelationValue sysRelationValue = new SysRelationValue();
            sysRelationValue.setBusinessId(RelationTypeEnum.BDFYSZ_MEMBER.getCode());
            sysRelationValue.setCode(RelationTypeEnum.EveryV1M.getCode());
            sysRelationValue.setValue(req.getEveryV1());
            sysRelationValueMapper.insert(sysRelationValue);
        }
        if(StringUtils.isNotEmpty(req.getEveryV2())){
            sysRelationValueMapper.delete(new LambdaQueryWrapper<SysRelationValue>()
                    .eq(SysRelationValue::getCode,RelationTypeEnum.EveryV2M.getCode() )
                    .eq(SysRelationValue::getBusinessId,RelationTypeEnum.BDFYSZ_MEMBER.getCode()));
            SysRelationValue sysRelationValue = new SysRelationValue();
            sysRelationValue.setBusinessId(RelationTypeEnum.BDFYSZ_MEMBER.getCode());
            sysRelationValue.setCode(RelationTypeEnum.EveryV2M.getCode());
            sysRelationValue.setValue(req.getEveryV2());
            sysRelationValueMapper.insert(sysRelationValue);
        }
        if(StringUtils.isNotEmpty(req.getEveryV3())){
            sysRelationValueMapper.delete(new LambdaQueryWrapper<SysRelationValue>()
                    .eq(SysRelationValue::getCode,RelationTypeEnum.EveryV3M.getCode() )
                    .eq(SysRelationValue::getBusinessId,RelationTypeEnum.BDFYSZ_MEMBER.getCode()));
            SysRelationValue sysRelationValue = new SysRelationValue();
            sysRelationValue.setBusinessId(RelationTypeEnum.BDFYSZ_MEMBER.getCode());
            sysRelationValue.setCode(RelationTypeEnum.EveryV3M.getCode());
            sysRelationValue.setValue(req.getEveryV3());
            sysRelationValueMapper.insert(sysRelationValue);
        }
        if(StringUtils.isNotEmpty(req.getEveryV4())){
            sysRelationValueMapper.delete(new LambdaQueryWrapper<SysRelationValue>()
                    .eq(SysRelationValue::getCode,RelationTypeEnum.EveryV4M.getCode() )
                    .eq(SysRelationValue::getBusinessId,RelationTypeEnum.BDFYSZ_MEMBER.getCode()));
            SysRelationValue sysRelationValue = new SysRelationValue();
            sysRelationValue.setBusinessId(RelationTypeEnum.BDFYSZ_MEMBER.getCode());
            sysRelationValue.setCode(RelationTypeEnum.EveryV4M.getCode());
            sysRelationValue.setValue(req.getEveryV4());
            sysRelationValueMapper.insert(sysRelationValue);
        }
        if(StringUtils.isNotEmpty(req.getEveryV5())){
            sysRelationValueMapper.delete(new LambdaQueryWrapper<SysRelationValue>()
                    .eq(SysRelationValue::getCode,RelationTypeEnum.EveryV5M.getCode() )
                    .eq(SysRelationValue::getBusinessId,RelationTypeEnum.BDFYSZ_MEMBER.getCode()));
            SysRelationValue sysRelationValue = new SysRelationValue();
            sysRelationValue.setBusinessId(RelationTypeEnum.BDFYSZ_MEMBER.getCode());
            sysRelationValue.setCode(RelationTypeEnum.EveryV5M.getCode());
            sysRelationValue.setValue(req.getEveryV5());
            sysRelationValueMapper.insert(sysRelationValue);
        }
        return 1;
    }



    @Transactional
    @Override
    public void imSaveOrUpdateYQfy(SysYQFYReq req) {

        if(StringUtils.isNotEmpty(req.getFirstOne())){
            sysRelationValueMapper.delete(new LambdaQueryWrapper<SysRelationValue>()
                    .eq(SysRelationValue::getCode,RelationTypeEnum.FirstOne.getCode() )
                    .eq(SysRelationValue::getBusinessId,RelationTypeEnum.YQFYSZ.getCode()));
            SysRelationValue sysRelationValue = new SysRelationValue();
            sysRelationValue.setBusinessId(RelationTypeEnum.YQFYSZ.getCode());
            sysRelationValue.setCode(RelationTypeEnum.FirstOne.getCode());
            sysRelationValue.setValue(req.getFirstOne());
            sysRelationValueMapper.insert(sysRelationValue);

        }
        if(StringUtils.isNotEmpty(req.getFirstTop())){
            sysRelationValueMapper.delete(new LambdaQueryWrapper<SysRelationValue>()
                    .eq(SysRelationValue::getCode,RelationTypeEnum.FirstTop.getCode() )
                    .eq(SysRelationValue::getBusinessId,RelationTypeEnum.YQFYSZ.getCode()));
            SysRelationValue sysRelationValue = new SysRelationValue();
            sysRelationValue.setBusinessId(RelationTypeEnum.YQFYSZ.getCode());
            sysRelationValue.setCode(RelationTypeEnum.FirstTop.getCode());
            sysRelationValue.setValue(req.getFirstTop());
            sysRelationValueMapper.insert(sysRelationValue);
        }

    }

    @Transactional
    @Override
    public void saveOrUpdateMdfy(SysMDFYReq req) {
        if(StringUtils.isNotEmpty(req.getOneMdfx())){
            sysRelationValueMapper.delete(new LambdaQueryWrapper<SysRelationValue>()
                    .in(SysRelationValue::getCode,RelationTypeEnum.MDFX_ONE.getCode()
                            ,RelationTypeEnum.MDFX_TWO.getCode()
                            ,RelationTypeEnum.MDFX_THREE.getCode())
                    .eq(SysRelationValue::getBusinessId,RelationTypeEnum.MDFYSZ.getCode()));
            List<SysRelationValue> list = new ArrayList<>();
            SysRelationValue oneValue = new SysRelationValue();
            oneValue.setBusinessId(RelationTypeEnum.MDFYSZ.getCode());
            oneValue.setCode(RelationTypeEnum.MDFX_ONE.getCode());
            oneValue.setValue(req.getOneMdfx());
            oneValue.setLabel(RelationTypeEnum.MDFX_ONE.getMsg());
            sysRelationValueMapper.insert(oneValue);

            SysRelationValue twoValue = new SysRelationValue();
            twoValue.setBusinessId(RelationTypeEnum.MDFYSZ.getCode());
            twoValue.setCode(RelationTypeEnum.MDFX_TWO.getCode());
            twoValue.setValue(req.getTwoMdfx());
            twoValue.setLabel(RelationTypeEnum.MDFX_TWO.getMsg());
            sysRelationValueMapper.insert(twoValue);

            SysRelationValue threeValue = new SysRelationValue();
            threeValue.setBusinessId(RelationTypeEnum.MDFYSZ.getCode());
            threeValue.setCode(RelationTypeEnum.MDFX_THREE.getCode());
            threeValue.setValue(req.getThreeMdfx());
            threeValue.setLabel(RelationTypeEnum.MDFX_THREE.getMsg());
            sysRelationValueMapper.insert(threeValue);
        }
        //购买第1单奖励
        if(StringUtils.isNotEmpty(req.getGmddjlOne())){
            sysRelationValueMapper.delete(new LambdaQueryWrapper<SysRelationValue>()
                    .eq(SysRelationValue::getCode,RelationTypeEnum.GMDDJL1.getCode() )
                    .eq(SysRelationValue::getBusinessId,RelationTypeEnum.MDFYSZ.getCode()));
            SysRelationValue sysRelationValue = new SysRelationValue();
            sysRelationValue.setBusinessId(RelationTypeEnum.MDFYSZ.getCode());
            sysRelationValue.setCode(RelationTypeEnum.GMDDJL1.getCode());
            sysRelationValue.setValue(req.getGmddjlOne());
            sysRelationValue.setLabel(RelationTypeEnum.GMDDJL1.getMsg());
            sysRelationValueMapper.insert(sysRelationValue);

        }
        //购买第2单奖励
        if(StringUtils.isNotEmpty(req.getGmddjlTwo())){
            sysRelationValueMapper.delete(new LambdaQueryWrapper<SysRelationValue>()
                    .eq(SysRelationValue::getCode,RelationTypeEnum.GMDDJL2.getCode() )
                    .eq(SysRelationValue::getBusinessId,RelationTypeEnum.MDFYSZ.getCode()));
            SysRelationValue sysRelationValue = new SysRelationValue();
            sysRelationValue.setBusinessId(RelationTypeEnum.MDFYSZ.getCode());
            sysRelationValue.setCode(RelationTypeEnum.GMDDJL2.getCode());
            sysRelationValue.setValue(req.getGmddjlTwo());
            sysRelationValue.setLabel(RelationTypeEnum.GMDDJL2.getMsg());
            sysRelationValueMapper.insert(sysRelationValue);

        }
        //购买第3单奖励
        if(StringUtils.isNotEmpty(req.getGmddjlThree())){
            sysRelationValueMapper.delete(new LambdaQueryWrapper<SysRelationValue>()
                    .eq(SysRelationValue::getCode,RelationTypeEnum.GMDDJL3.getCode() )
                    .eq(SysRelationValue::getBusinessId,RelationTypeEnum.MDFYSZ.getCode()));
            SysRelationValue sysRelationValue = new SysRelationValue();
            sysRelationValue.setBusinessId(RelationTypeEnum.MDFYSZ.getCode());
            sysRelationValue.setCode(RelationTypeEnum.GMDDJL3.getCode());
            sysRelationValue.setValue(req.getGmddjlThree());
            sysRelationValue.setLabel(RelationTypeEnum.GMDDJL3.getMsg());
            sysRelationValueMapper.insert(sysRelationValue);

        }
    }

    @Override
    @Transactional
    public void saveOrUpdateBdsy(SysAreaOrderReq req) {
        SysAreaOrder order;
        if(req.getId()!=null){
            order = sysAreaOrderMapper.selectById(req.getId());
            BeanUtils.copyProperties(req,order);
            order.setId(req.getId());
            order.setUpdateBy(getUsername());
            order.setUpdateTime(new Date());
            sysAreaOrderMapper.updateById(order);
            //更新
            List<AssetEvidence> assetEvidences = assetEvidenceMapper.selectList(new LambdaQueryWrapper<AssetEvidence>()
                    .eq(AssetEvidence::getArea, req.getAreaType()));
            if(assetEvidences!=null && assetEvidences.size()>0){
                AssetEvidence assetEvidence = assetEvidences.get(0);
                assetEvidence.setName(req.getMoney()+"个资产通证");
                assetEvidence.setPrice(req.getMoney());
                assetEvidence.setUseFlag(req.getUseFlag());
                assetEvidence.setNum(req.getMoney());
                assetEvidence.setUpdateTime(new Date());
                assetEvidenceMapper.updateById(assetEvidence);
            }

        }else{
            order = new SysAreaOrder();
            BeanUtils.copyProperties(req,order);
            order.setUpdateBy(getUsername());
            order.setUpdateTime(new Date());
            sysAreaOrderMapper.insert(order);
            //
            AssetEvidence evidence = new AssetEvidence();
            evidence.setName(req.getMoney()+"个资产通证");
            evidence.setNum(req.getMoney());
            evidence.setArea(req.getAreaType());
            evidence.setPrice(req.getMoney());
            evidence.setUseFlag(req.getUseFlag());
            assetEvidenceMapper.insert(evidence);

        }
    }

    @Override
    public void bannerSaveOrUpdate(SysBannerImgaeReq req) {
        if(StringUtils.isEmpty(req.getImageUrl())){
            throw new ServiceException("图片路径不能未空！");
        }
        SysRelationValue sysRelationValue = null;
        if(StringUtils.isNotEmpty(req.getId())){
            sysRelationValue = sysRelationValueMapper.selectById(req.getId());
        }else{
            sysRelationValue = new SysRelationValue();
        }
        sysRelationValue.setValue(req.getImageUrl());
        sysRelationValue.setCode(RelationTypeEnum.BANNER_URL.getCode());
        sysRelationValue.setBusinessId(RelationTypeEnum.BANNER_URL.getCode());
        if(StringUtils.isNotEmpty(req.getId())){
            sysRelationValueMapper.updateById(sysRelationValue);
        }else{
            sysRelationValueMapper.insert(sysRelationValue);
        }

    }

    @Override
    public int deleteBannerById(Long id) {
        return sysRelationValueMapper.deleteById(id);
    }

    @Override
    public AjaxResult getBannerInfo() {
        List<SysRelationValue> list = sysRelationValueMapper.selectList(new LambdaQueryWrapper<SysRelationValue>()
                .eq(SysRelationValue::getCode, RelationTypeEnum.BANNER_URL.getCode())
                .orderByAsc(SysRelationValue::getId));

        if(list!=null && list.size()>0 ){
            list.stream().forEach(c->{
                List<SysRelationValue> sysRelationValues = sysRelationValueMapper.selectList(new LambdaQueryWrapper<SysRelationValue>()
                        .eq(SysRelationValue::getCode, RelationTypeEnum.BANNER_URL_SET.getCode())
                        .eq(SysRelationValue::getBusinessId, c.getId()));
                if(sysRelationValues!=null && sysRelationValues.size()>0){
                    c.setSortNum(Integer.parseInt(sysRelationValues.get(0).getLabel()));
                }
            });
        }
        return AjaxResult.success(list);
    }

    @Override
    @Transactional
    public void setBannerImage(SysBannerImgaeReq req) {

        if(StringUtils.isEmpty(req.getId())){
            throw new ServiceException("未上传id");
        }
        if(req.getSortNum() == null){
            throw new ServiceException("未上传轮播序号");
        }
        if(req.getImageUrl() == null){
            throw new ServiceException("未上传轮播图片路径");
        }
        //先删后增
        sysRelationValueMapper.delete(new LambdaQueryWrapper<SysRelationValue>()
        .eq(SysRelationValue::getCode,RelationTypeEnum.BANNER_URL_SET.getCode())
        .eq(SysRelationValue::getBusinessId,req.getId()));

        sysRelationValueMapper.delete(new LambdaQueryWrapper<SysRelationValue>()
                .eq(SysRelationValue::getCode,RelationTypeEnum.BANNER_URL_SET.getCode())
                .eq(SysRelationValue::getLabel,req.getSortNum()));

        SysRelationValue value = new SysRelationValue();
        value.setValue(req.getImageUrl());
        value.setBusinessId(req.getId());
        value.setLabel(req.getSortNum().toString());
        value.setCode(RelationTypeEnum.BANNER_URL_SET.getCode());
        sysRelationValueMapper.insert(value);
    }

    @Override
    public List<SysRelationValue> getAppBannerInfo() {
        return sysRelationValueMapper.selectList(new LambdaQueryWrapper<SysRelationValue>()
                .eq(SysRelationValue::getCode, RelationTypeEnum.BANNER_URL_SET.getCode())
                .in(SysRelationValue::getLabel,Arrays.asList("1","2","3"))
                .orderByDesc(SysRelationValue::getLabel));
    }


    /**
     * 修改【请填写功能名称】
     *
     * @param sysRelationValue 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateSysRelationValue(SysRelationValue sysRelationValue)
    {
        return sysRelationValueMapper.updateSysRelationValue(sysRelationValue);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteSysRelationValueByIds(Long[] ids)
    {
        return sysRelationValueMapper.deleteSysRelationValueByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteSysRelationValueById(Long id)
    {
        return sysRelationValueMapper.deleteSysRelationValueById(id);
    }

    @Override
    @Transactional
    public int zcglSaveOrUpdate(ZCGLReq req) {

        SysRelationValue sysRelationValue = new SysRelationValue();
        //资产管理
        sysRelationValue.setBusinessId(RelationTypeEnum.company_value_manage.getCode());
        if(StringUtils.isNotEmpty(req.getZzcZzc())){
            //总资产
            sysRelationValue.setCode(RelationTypeEnum.ZZC_zzc.getCode());
            sysRelationValue.setLabel(RelationTypeEnum.ZZC_zzc.getMsg());
            sysRelationValue.setValue(req.getZzcZzc());
        }else
          //今日增加
        if(StringUtils.isNotEmpty(req.getZzcJrzj())){
            sysRelationValue.setCode(RelationTypeEnum.ZZC_jrzj.getCode());
            sysRelationValue.setLabel(RelationTypeEnum.ZZC_jrzj.getMsg());

            sysRelationValue.setValue(req.getZzcJrzj());
        }else

        //品牌价值
        if(StringUtils.isNotEmpty(req.getPpjzPpzz())){
            sysRelationValue.setCode(RelationTypeEnum.PPJZ_ppzz.getCode());
            sysRelationValue.setLabel(RelationTypeEnum.PPJZ_ppzz.getMsg());

            sysRelationValue.setValue(req.getPpjzPpzz());
        }else
        //本月增值
        if(StringUtils.isNotEmpty(req.getPpjzByzz())){
            sysRelationValue.setCode(RelationTypeEnum.PPJZ_byzz.getCode());
            sysRelationValue.setLabel(RelationTypeEnum.PPJZ_byzz.getMsg());
            sysRelationValue.setValue(req.getPpjzByzz());
        }

        else
        if(StringUtils.isNotEmpty(req.getZctzZctz())){
            //资产通证
            sysRelationValue.setCode(RelationTypeEnum.ZCTZ_zctz.getCode());
            sysRelationValue.setLabel(RelationTypeEnum.ZCTZ_zctz.getMsg());
            sysRelationValue.setValue(req.getZctzZctz());
        }
        //先删除，再保存
        SysRelationValue cond = new SysRelationValue();
        cond.setBusinessId(sysRelationValue.getBusinessId());
        cond.setCode(sysRelationValue.getCode());
        if(StringUtils.isEmpty(cond.getCode())){
            throw new ServiceException("参数错误");
        }
        sysRelationValueMapper.deleteSysRelationValue(cond);
        //再保存
        int insert = sysRelationValueMapper.insert(sysRelationValue);
        return insert;
    }

    @Override
    public AjaxResult zcglInfo() {

        List<SysRelationValue> sysRelationValues = sysRelationValueMapper.selectList(new LambdaQueryWrapper<SysRelationValue>()
                .eq(SysRelationValue::getBusinessId, RelationTypeEnum.company_value_manage.getCode()));
        HashMap<String, String> map = new HashMap<>();
        if(sysRelationValues!=null && sysRelationValues.size()>0){
            Map<String, List<SysRelationValue>> collect = sysRelationValues.stream().collect(Collectors.groupingBy(SysRelationValue::getCode));
            collect.forEach((k,v)->{
                if(v!=null && v.size()>0){
                    map.put(k,v.get(0).getValue());
                }

            });
            map.put(RelationTypeEnum.ZZC_zzc.getCode(),MapUtils.getString(map,RelationTypeEnum.ZZC_zzc.getCode(),"0"));
            map.put(RelationTypeEnum.ZZC_jrzj.getCode(),MapUtils.getString(map,RelationTypeEnum.ZZC_jrzj.getCode(),"0"));

            //做计算
            //（1）现金底池
            //金额=店面增值+会员贡献
            map.put(RelationTypeEnum.cash_store_add.getCode(),MapUtils.getString(map,RelationTypeEnum.cash_store_add.getCode(),"0"));
            map.put(RelationTypeEnum.cash_member_add.getCode(),MapUtils.getString(map,RelationTypeEnum.cash_member_add.getCode(),"0"));
            String cashStoreAdd = MapUtils.getString(map,RelationTypeEnum.cash_store_add.getCode(),"0");
            String cashMemberAdd = MapUtils.getString(map,RelationTypeEnum.cash_member_add.getCode(),"0");
            BigDecimal cashMoney = new BigDecimal(cashMemberAdd).add(new BigDecimal(cashStoreAdd));
            map.put(RelationTypeEnum.cash_money.getCode(),cashMoney.toString());

            map.put(RelationTypeEnum.PPJZ_ppzz.getCode(),MapUtils.getString(map,RelationTypeEnum.PPJZ_ppzz.getCode(),"0"));
            map.put(RelationTypeEnum.PPJZ_byzz.getCode(),MapUtils.getString(map,RelationTypeEnum.PPJZ_byzz.getCode(),"0"));
            map.put(RelationTypeEnum.brand_gj_score.getCode(),MapUtils.getString(map,RelationTypeEnum.brand_gj_score.getCode(),"0"));
            map.put(RelationTypeEnum.brand_gj_add.getCode(),MapUtils.getString(map,RelationTypeEnum.brand_gj_add.getCode(),"0"));
            map.put(RelationTypeEnum.ZCTZ_zctz.getCode(),MapUtils.getString(map,RelationTypeEnum.ZCTZ_zctz.getCode(),"0"));
            map.put(RelationTypeEnum.ZCTZ_member_use.getCode(),MapUtils.getString(map,RelationTypeEnum.ZCTZ_member_use.getCode(),"0"));
            map.put(RelationTypeEnum.ZCTZ_sy.getCode(),MapUtils.getString(map,RelationTypeEnum.ZCTZ_sy.getCode(),"0"));






        }
        return AjaxResult.success(map);
    }

    @Override
    public AjaxResult yqfyInfo() {
        List<SysRelationValue> sysRelationValues = sysRelationValueMapper.selectList(new LambdaQueryWrapper<SysRelationValue>()
                .eq(SysRelationValue::getBusinessId, RelationTypeEnum.YQFYSZ.getCode()));
        HashMap<String, Object> map = new HashMap<>();
        if(sysRelationValues!=null && sysRelationValues.size()>0){
            Map<String, List<SysRelationValue>> collect = sysRelationValues.stream().collect(Collectors.groupingBy(SysRelationValue::getCode));
            collect.forEach((k,v)->{
                if(v!=null && v.size()>0){
                    map.put(k,v.get(0).getValue());
                }

            });
        }
        return AjaxResult.success(map);
    }

    @Override
    public AjaxResult mdfyInfo() {
        List<SysRelationValue> sysRelationValues = sysRelationValueMapper.selectList(new LambdaQueryWrapper<SysRelationValue>()
                .eq(SysRelationValue::getBusinessId, RelationTypeEnum.MDFYSZ.getCode()));
        HashMap<String, Object> map = new HashMap<>();
        if(sysRelationValues!=null && sysRelationValues.size()>0){
            Map<String, List<SysRelationValue>> collect = sysRelationValues.stream().collect(Collectors.groupingBy(SysRelationValue::getCode));
            collect.forEach((k,v)->{
                if(v!=null && v.size()>0){
                    map.put(k,v.get(0).getValue());
                }

            });
        }
        return AjaxResult.success(map);
    }


    @Override
    public AjaxResult bdfyInfo() {
        List<SysRelationValue> sysRelationValues =
                sysRelationValueMapper.selectList(new LambdaQueryWrapper<SysRelationValue>()
                .eq(SysRelationValue::getBusinessId, RelationTypeEnum.BDFYSZ.getCode()));
        HashMap<String, Object> map = new HashMap<>();
        if(sysRelationValues!=null && sysRelationValues.size()>0){
            Map<String, List<SysRelationValue>> collect = sysRelationValues.stream().collect(Collectors.groupingBy(SysRelationValue::getCode));
            collect.forEach((k,v)->{
                if(v!=null && v.size()>0){
                    map.put(k,v.get(0).getValue());
                }

            });
        }
        return AjaxResult.success(map);
    }

    /**
     * @description: 保单返佣 -会员
     * @author: ljx
     * @date: 2023/6/1 23:06
     * @param: []
     * @return: com.lyh.common.core.domain.AjaxResult
     **/

    @Override
    public AjaxResult bdfyInfoMember() {
        List<SysRelationValue> sysRelationValues =
                sysRelationValueMapper.selectList(new LambdaQueryWrapper<SysRelationValue>()
                        .eq(SysRelationValue::getBusinessId, RelationTypeEnum.BDFYSZ_MEMBER.getCode()));
        HashMap<String, Object> map = new HashMap<>();
        if(sysRelationValues!=null && sysRelationValues.size()>0){
            Map<String, List<SysRelationValue>> collect = sysRelationValues.stream().collect(Collectors.groupingBy(SysRelationValue::getCode));
            collect.forEach((k,v)->{
                if(v!=null && v.size()>0){
                    map.put(k,v.get(0).getValue());
                }
            });
        }
        return AjaxResult.success(map);
    }

    @Override
    public AjaxResult bdsyInfo() {
        List<SysAreaOrder> sysAreaOrders = sysAreaOrderService.listInfo();
        return AjaxResult.success(sysAreaOrders);
    }

    @Override
    public AppUpdateVo getAppUpdate() {
        return sysRelationValueMapper.getAppUpdate();
    }

    /**
     * 获取登录用户名
     */
    public String getUsername()
    {
        return getLoginUser().getUsername();
    }
}
